package com.es.www.kyl.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.es.www.kyl.constant.BusinessResultEnum;
import com.es.www.kyl.constant.RedisConstants;
import com.es.www.kyl.exception.ResException;
import com.es.www.kyl.service.SmsService;
import com.es.www.kyl.util.RedisUtils;
import com.es.www.kyl.util.SmsUtil;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author estranger
 */
@Slf4j
@Service("SmsService")
@RequiredArgsConstructor
public class SmsServiceImpl implements SmsService {

    private final RedisUtils redisUtils;

    @Override
    public String getVerifyCode(String userPhone) {
        //获取验证码的key
        String smsKey = RedisConstants.SMS_VERIFY_CODE_KEY_PREFIX + userPhone;
        Captcha captcha = new SpecCaptcha(111, 36, 4);
        captcha.setCharType(Captcha.TYPE_ONLY_NUMBER);
        //设置过期时间2分钟
        redisUtils.setStringValue(smsKey, captcha.text(), 2, TimeUnit.MINUTES);
        return captcha.toBase64();
    }
}
